
Page({
  data: {
    isAnalyzing: false, // 是否正在分析
    analysisResult: '' // 分析结果
  },

  async aiText() {
    if (this.data.isAnalyzing) return;
    this.setData({ isAnalyzing: true });
  
    try {
      const result = wx.getStorageSync('dataBaseResult');
      if (!result) {
        throw new Error('未找到数据');
      }
  
      const model = wx.cloud.extend.AI.createModel("hunyuan-exp");
      const res = await model.streamText({
        data: {
          model: "hunyuan-turbos-latest",
          messages: [
            {
              role: "user",
              content: '请帮我生成一份健康报告，报告内容应在500字以内且包括以下分析：\n1. 健康建议\n数据如下：' + JSON.stringify(result)
            }
          ]
        }
      });
  
      let text = '';
      for await (let str of res.textStream) {
        text += str;
        this.setData({ analysisResult: text });
      }
    } catch (error) {
      console.error('AI分析失败:', error);
      wx.showToast({
        title: '分析失败，请重试',
        icon: 'none'
      });
    } finally {
      this.setData({ isAnalyzing: false });
    }
  },




});